//
//  NSObject+Tool.m
//

#import "NSObject+Tool.h"
#import "UIView+Frame.h"

@implementation NSObject (Tool)

- (UIViewController *)currentViewController{

    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        keyWindow =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    
    if ([keyWindow.rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tab =(UITabBarController *) keyWindow.rootViewController;
        if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
            return [nav.viewControllers lastObject];
        } else {
            return tab.selectedViewController;
        }
    }
    else if ([keyWindow.rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)keyWindow.rootViewController;
        return [nav.viewControllers lastObject];
    }
    return nil;
    
}

- (UIViewController *)getCurrentVC{
    UIViewController *resultVC = nil;
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    if (@available(iOS 14,*)) {
        keyWindow =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    }
    
    if (keyWindow.windowLevel != UIWindowLevelNormal) {
        NSArray *windowAry = [UIApplication sharedApplication].windows;
        for (UIWindow *win in windowAry) {
            if (win.windowLevel == UIWindowLevelNormal) {
                keyWindow = win;
                break;
            }
        }
    }
    resultVC = keyWindow.rootViewController;
    
    
    while (resultVC.presentedViewController) {
        resultVC = resultVC.presentedViewController;
    }
    
    if ([resultVC isKindOfClass:[UITabBarController class]]) {
        resultVC = [(UITabBarController *)resultVC selectedViewController];
    }
    
    if ([resultVC isKindOfClass:[UINavigationController class]]) {
        resultVC = [(UINavigationController *)resultVC visibleViewController];
    }
    
    return resultVC;
}

+(BOOL)isIPhoneX{
    BOOL result = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return result;
    }
    if (@available(iOS 11.0, *)) {
        CGSize size = [UIScreen mainScreen].bounds.size;
        NSInteger notchValue = size.width / size.height * 100;
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0 || notchValue == 216 || notchValue == 46) {
            result = YES;
        }
    }
    return result;
}

+ (CGFloat)vg_safeDistanceTop {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.top;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.top;
    }
    return 0;
}

+ (CGFloat)vg_safeDistanceBottom {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.bottom;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.bottom;
    }
    return 0;
}

+ (CGFloat)vg_statusBarHeight {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
        return statusBarManager.statusBarFrame.size.height;
    } else {
        return [UIApplication sharedApplication].statusBarFrame.size.height;
    }
}

+ (CGFloat)vg_navigationBarHeight {
    return 44.0f;
}

+ (CGFloat)vg_navigationFullHeight {
    return [NSObject vg_statusBarHeight] + [NSObject vg_navigationBarHeight];
}

+ (CGFloat)vg_tabBarHeight {
    return 49.0f;
}

+ (CGFloat)vg_tabBarFullHeight {
    return [NSObject vg_tabBarHeight] + [NSObject vg_safeDistanceBottom];
}


@end

